package com.bobi.tfca.ui.mainnavigation.message.notification

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentPublishNotificationBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.RequestBody

class PublishNotificationFragment : BaseFragment<FragmentPublishNotificationBinding, ViewModel>(
    FragmentPublishNotificationBinding::inflate,
    null,
    true
) {
    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentPublishNotificationBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 设置头部菜单的按钮
        setTopPanelStyle(binding)

    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentPublishNotificationBinding) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }

        // 右边按钮
        val rightButton = topPanel.rightButton
        rightButton.text = "确定"
        rightButton.setOnClickListener {
            val notificationContent = binding.notificationContent.text.toString()
            if (notificationContent.isBlank()) {
                LogUtil.e("通知内容为空")
            } else {
                val requestBody = RequestBody.create(MediaType.parse("text/plain"), notificationContent)
                // 发布通知，暂时只实现全体通知
                publicViewModel!!.apply {
                    request(MessageAPI::class.java).publishNotification(requestBody).getResponse {
                        it.collect {
                            when (it) {
                                is ApiResponse.Error -> {
                                    LogUtil.e(it.errMsg)
                                }

                                ApiResponse.Loading -> LogUtil.e("Loading")
                                is ApiResponse.Success -> {
                                    withContext(Dispatchers.Main) {
                                        val result = it.data
                                        if (result?.code == 200) {
                                            // 发布通知成功，发出提示
                                            Toast.makeText(requireActivity(), "发布成功", Toast.LENGTH_SHORT).show()
                                            // 跳转
                                            requireActivity().findNavController(R.id.app_nav).navigateUp()
                                        } else {
                                            Toast.makeText(requireActivity(), "发布失败，请输入正确的用户ID", Toast.LENGTH_SHORT).show()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // 中间标题
        val title = topPanel.titleText
        title.text = "发布通知"
    }
}